package init

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"order_srv/basic/config"
	"order_srv/handler/models"
)

func InitMysql() {
	var err error
	cfg := config.AppCong.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		cfg.User, cfg.Password, cfg.Host, cfg.Port, cfg.Database)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})

	if err != nil {
		panic(err)
	}
	log.Println("mysql inits success")
	err = config.DB.AutoMigrate(&models.Order{}, &models.OrderItem{}, &models.OrderPayment{}, &models.OrderReview{},
		&models.OrderComplaint{}, &models.OrderContract{}, &models.OrderMessage{}, &models.OrderStatusLog{},
		&models.OrderAcceptance{}, &models.Operator{}, &models.OperationLog{})
	if err != nil {
		panic(err)
	}
}
